#!/bin/sh

### BEGIN INIT INFO
# Provides:             ucarp
# Required-Start:       $syslog
# Required-Stop:        $syslog
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    UCARP - failover IP
### END INIT INFO

# v 0.2 - 20100630
# by Fabien Duchene
# f.duchene@car-online.fr
###################
# NEW FEATURES
# - support for multi VIP (serveral configuration files)
###################
# CORRECTED BUGS:
# - when stopping, not enough checks were made


# v 0.1 - 20100202
# by Erwan 'Labynocle' Ben Souiden
# erwan@aleikoum.net

CONF__PHYSICAL_INTERFACE="eth0"
CONF__SUB_IF_NUMBER=1


SUB_INTERFACE="${CONF__PHYSICAL_INTERFACE}:${CONF__SUB_IF_NUMBER}"
SUB_IF_STRING="${CONF__PHYSICAL_INTERFACE}_${CONF__SUB_IF_NUMBER}"

CONFIG__MAX_NUM_OF_STOP_TRIES=4
CONFIG__SLEEP_TIME_BTWN_OPERATIONS=1

UCARP_BIN='/usr/sbin/ucarp'
UCARP_PID_FILE="/var/run/ucarp_${SUB_IF_STRING}.pid"
UCARP_CONF_FILE="/etc/ucarp/${SUB_IF_STRING}/ucarp.conf"

# some basic checks 
[ ! -x $UCARP_BIN ] && echo "ERROR - $UCARP_BIN doesn't exist or not executable !" && exit 1
[ ! -x "$UCARP_CONF_FILE" ] && echo "ERROR - $UCARP_CONF_FILE doesn't exist or not executable !" && exit 1

. $UCARP_CONF_FILE


case "$1" in
	start)
		echo "Starting UCARP as a daemon with $UCARP_CONF_FILE as configuration file..."

		[ -f $UCARP_PID_FILE ] && echo "ERROR - $UCARP_PID_FILE exists, maybe ucarp is still running" && exit 1

		if [ "$UCARP_MASTER" = 'yes' ]
		then
			[ $DEBUG_VALUE -ge 1 ] && echo "DEBUG - $UCARP_BIN -i $UCARP_INTERFACE -b $UCARP_ADV -s $UCARP_ADDR_PHY -v $UCARP_VID -o $UCARP_PASSWD_FILE -a $UCARP_ADDR_VIR -u $UCARP_UP_INT_SCRIPT -d $UCARP_DOWN_INT_SCRIPT -z -P -B"
			$UCARP_BIN -i $UCARP_INTERFACE -b $UCARP_ADV -s $UCARP_ADDR_PHY -v $UCARP_VID -o $UCARP_PASSWD_FILE -a $UCARP_ADDR_VIR -u $UCARP_UP_INT_SCRIPT -d $UCARP_DOWN_INT_SCRIPT -z -P -B
		else
			[ $DEBUG_VALUE -ge 1 ] && echo "DEBUG - $UCARP_BIN -i $UCARP_INTERFACE -b $UCARP_ADV -s $UCARP_ADDR_PHY -v $UCARP_VID -o $UCARP_PASSWD_FILE -a $UCARP_ADDR_VIR -u $UCARP_UP_INT_SCRIPT -d $UCARP_DOWN_INT_SCRIPT -z -B -k $UCARP_ADVSKEW"
			$UCARP_BIN -i $UCARP_INTERFACE -b $UCARP_ADV -s $UCARP_ADDR_PHY -v $UCARP_VID -o $UCARP_PASSWD_FILE -a $UCARP_ADDR_VIR -u $UCARP_UP_INT_SCRIPT -d $UCARP_DOWN_INT_SCRIPT -z -B -k $UCARP_ADVSKEW
		fi

		[ $? -ne 0 ] && echo "Starting UCARP as a daemon...FAILED" && exit 1
		pidof -s "$UCARP_BIN" > $UCARP_PID_FILE
		chmod 700 $UCARP_PID_FILE
		echo "Starting UCARP as a daemon (pid : `cat $UCARP_PID_FILE`)...OK"
	;;
	start-nodaemon)
		echo "Starting UCARP with $UCARP_CONF_FILE as configuration file..."
		if [ "$UCARP_MASTER" = 'yes' ]
		then
			[ $DEBUG_VALUE -ge 1 ] && echo "DEBUG - $UCARP_BIN -i $UCARP_INTERFACE -b $UCARP_ADV -s $UCARP_ADDR_PHY -v $UCARP_VID -o $UCARP_PASSWD_FILE -a $UCARP_ADDR_VIR -u $UCARP_UP_INT_SCRIPT -d $UCARP_DOWN_INT_SCRIPT -z -P"
			$UCARP_BIN -i $UCARP_INTERFACE -b $UCARP_ADV -s $UCARP_ADDR_PHY -v $UCARP_VID -o $UCARP_PASSWD_FILE -a $UCARP_ADDR_VIR -u $UCARP_UP_INT_SCRIPT -d $UCARP_DOWN_INT_SCRIPT -z -P
		else
			[ $DEBUG_VALUE -ge 1 ] && echo "DEBUG - $UCARP_BIN -i $UCARP_INTERFACE -b $UCARP_ADV -s $UCARP_ADDR_PHY -v $UCARP_VID -o $UCARP_PASSWD_FILE -a $UCARP_ADDR_VIR -u $UCARP_UP_INT_SCRIPT -d $UCARP_DOWN_INT_SCRIPT -z -k $UCARP_ADVSKEW"
			$UCARP_BIN -i $UCARP_INTERFACE -b $UCARP_ADV -s $UCARP_ADDR_PHY -v $UCARP_VID -o $UCARP_PASSWD_FILE -a $UCARP_ADDR_VIR -u $UCARP_UP_INT_SCRIPT -d $UCARP_DOWN_INT_SCRIPT -z -k $UCARP_ADVSKEW
		fi
	;;
	stop)
		echo "Stopping UCARP daemon for sub-interface $SUB_INTERFACE"
		if [ -f "$UCARP_PID_FILE" ]
		then
			DAEMON_PID=`cat $UCARP_PID_FILE`
			echo "(pid : `cat $UCARP_PID_FILE`)"
			
			i=0
			stop=""
			while [ $i -lt $CONFIG__MAX_NUM_OF_STOP_TRIES ] && [ -z "$stop" ]
			do
				i=$[$i+1]
				echo "try: $i"
				kill -15 $DAEMON_PID
				sleep ${CONFIG__SLEEP_TIME_BTWN_OPERATIONS}
				rm -f $UCARP_PID_FILE

				if [ -f "$UCARP_PID_FILE" ]
				then
					echo "error"
				else
					echo "OK"
					stop=1
				fi
			done
		else
			echo "not running"
		fi
	;;
	status)
		if [ -f "$UCARP_PID_FILE" ]
		then
			echo "UCARP is running (pid : `cat $UCARP_PID_FILE`)"
		else
			echo "UCARP is not running"
		fi
	;;
	restart|force-reload)
		$0 stop
		sleep 5
		$0 start
	;;
	*)
		echo "Usage: $0 {start|start-nodaemon|stop|status|restart|force-reload}"
		exit 1
	;;
	esac
exit 0

